//
//  SDGameManager.m
//  Sudoku
//
//  Created by wxlan on 16/6/13.
//  Copyright © 2016年 wxlan. All rights reserved.
//

#import "SDGameManager.h"
#define kGameDataPath  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"gameData.data"]

@interface SDGameManager ()
@property (nonatomic, strong) NSMutableArray *gameDataArray;
@end

@implementation SDGameManager

static id instance;

- (NSMutableArray *)gameDataArray
{
    if(_gameDataArray == nil)
    {
        _gameDataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:kGameDataPath];
        if (_gameDataArray == nil) {
            _gameDataArray = [NSMutableArray arrayWithArray:[self getOriginGameData]];
            [NSKeyedArchiver archiveRootObject:_gameDataArray toFile:kGameDataPath];
        }
    }
    return _gameDataArray;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (instance == nil) {
            instance = [super allocWithZone:zone];
        }
    });
    return instance;
}
+ (instancetype)sharedSDGameManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}
- (id)copyWithZone:(NSZone *)zone {
    return instance;
}

// 读取游戏初始化plist文件
- (NSArray *)getOriginGameData
{
//    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Level.plist" ofType:nil];
//    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Level1.txt" ofType:nil] encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [textFileContents componentsSeparatedByString:@"\n"];
    NSMutableArray *easyArray = [NSMutableArray array];
    NSMutableArray *normalArray = [NSMutableArray array];
    NSMutableArray *hardArray = [NSMutableArray array];
    
    
    for (NSString *str in array) {
        NSString *tempStr = [str substringFromIndex:str.length - 81];
        if ([str containsString:@"初级"]) {
            [easyArray addObject:@{@"originNumber":tempStr}];
        }else if([str containsString:@"中级"])
        {
            [normalArray addObject:@{@"originNumber":tempStr}];
        }else if([str containsString:@"高级"])
        {
            [hardArray addObject:@{@"originNumber":tempStr}];
            
        }
    }
    NSArray *dataArray = [[NSArray alloc] initWithObjects:easyArray,normalArray,hardArray, nil];

    return dataArray;
//    NSArray *levelArray = [SDLevelDetailModel mj_objectArrayWithKeyValuesArray:dict[@"Easy"]];
}

// 获取某一个游戏数据
- (SDLevelDetailModel *)getOneGameDataWithIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *array = self.gameDataArray[indexPath.section];
    if (indexPath.row > array.count) {
        return nil;
    }
    return [SDLevelDetailModel mj_objectWithKeyValues:array[indexPath.row]];

}


// 保存某一游戏数据
- (void)saveOneGameDataWithIndexPath:(NSIndexPath *)indexPath andGameModel:(SDLevelDetailModel *)gameModel
{
    
    NSMutableArray *gameArray = self.gameDataArray;
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:gameArray[indexPath.section]];
    [array replaceObjectAtIndex:indexPath.row withObject:[gameModel mj_keyValues]];
    gameArray[indexPath.section] = array;
    [NSKeyedArchiver archiveRootObject:gameArray toFile:kGameDataPath];
    
    
}

// 题库题目数量

- (NSInteger)numberOfAskAtSection:(NSInteger)section
{
    return [self.gameDataArray[section] count];
}

@end
